﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class ZhiJiWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.ZhiJiModel model = new EmpbasicService.ZhiJiModel();
        public int ID;
        public bool IsNew = false;
        public ZhiJiWindow(int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增职务层次信息";
                model = new EmpbasicService.ZhiJiModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑职务层次信息";
                Client.GetZhiJiModelCompleted += new EventHandler<EmpbasicService.GetZhiJiModelCompletedEventArgs>(Client_GetZhiJiModelCompleted);
                Client.GetZhiJiModelAsync(ID);
            }
        }

        void Client_GetZhiJiModelCompleted(object sender, EmpbasicService.GetZhiJiModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                Client.AddZhiJiModelCompleted += new EventHandler<EmpbasicService.AddZhiJiModelCompletedEventArgs>(Client_AddZhiJiModelCompleted);
                Client.AddZhiJiModelAsync(model);
            }
            else
            {
                Client.UpdateZhiJiModelCompleted += new EventHandler<EmpbasicService.UpdateZhiJiModelCompletedEventArgs>(Client_UpdateZhiJiModelCompleted);
                Client.UpdateZhiJiModelAsync(model);
            }
        }

        void Client_UpdateZhiJiModelCompleted(object sender, EmpbasicService.UpdateZhiJiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddZhiJiModelCompleted(object sender, EmpbasicService.AddZhiJiModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                double.Parse(((TextBox)sender).Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                ((TextBox)sender).Text = "";
            }
        }
    }
}
